package com.skyline.spot.mapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

import com.skyline.base.type.Activity;
import com.skyline.base.type.SpotType;
import com.skyline.spot.model.Spot;

public class SpotMapper implements RowMapper<Spot> {

	private final static String COLUMN_ID = "id";
	private final static String COLUMN_NAME = "name";
	private final static String COLUMN_PORTRAIT = "portrait";
	private final static String COLUMN_CATEGORY = "category";

	private final static String COLUMN_CREATORID = "creatorId";
	private final static String COLUMN_CREATORNICKNAME = "creatorNickname";
	private final static String COLUMN_CREATOREMAIL = "creatorEmail";
	private final static String COLUMN_CREATORPORTRAIT = "creatorPortrait";

	private final static String COLUMN_ADMINISTRATORID = "administratorId";
	private final static String COLUMN_ADMINISTRATORNICKNAME = "administratorNickname";
	private final static String COLUMN_ADMINISTRATOREMAIL = "administratorEmail";
	private final static String COLUMN_ADMINISTRATORPORTRAIT = "administratorPortrait";

	private final static String COLUMN_CREATETIME = "createTime";
	private final static String COLUMN_UPDATETIME = "updateTime";

	private final static String COLUMN_INTRODUCTION = "introduction";

	private final static String COLUMN_COUNTRY = "country";
	private final static String COLUMN_PROVINCE = "province";
	private final static String COLUMN_CITY = "city";
	private final static String COLUMN_SITE_ID = "siteid";

	private final static String COLUMN_DETAILADDRESS = "detailAddress";
	private final static String COLUMN_LATITUDE = "latitude";
	private final static String COLUMN_LONGITUDE = "longitude";

	private final static String COLUMN_VISTNUM = "visitnum";
	private final static String COLUMN_STAR = "star";
	private final static String COLUMN_VOTES = "votes";

	private final static String COLUMN_ACTIVITY = "activity";

	private final static SpotMapper MAPPER = new SpotMapper();

	public static SpotMapper getMapper() {
		return MAPPER;
	}

	public Spot mapRow(ResultSet rs, int rowNum) throws SQLException {
		Spot s = new Spot();

		try {
			s.setId(rs.getLong(COLUMN_ID));
		} catch (Exception e) {

		}
		try {
			s.setName(rs.getString(COLUMN_NAME));
			s.setPortrait(rs.getString(COLUMN_PORTRAIT));
			s.setCategory(SpotType.valueOf(rs.getString(COLUMN_CATEGORY)));
		} catch (Exception e) {

		}
		try {
			s.setCreatorId(rs.getLong(COLUMN_CREATORID));
			s.setCreatorNickname(rs.getString(COLUMN_CREATORNICKNAME));
			s.setCreatorEmail(rs.getString(COLUMN_CREATOREMAIL));
			s.setCreatorPortrait(rs.getString(COLUMN_CREATORPORTRAIT));
		} catch (Exception e) {

		}
		try {
			s.setAdministratorId(rs.getLong(COLUMN_ADMINISTRATORID));
			s.setAdministratorNickname(rs
					.getString(COLUMN_ADMINISTRATORNICKNAME));
			s.setAdministratorEmail(rs.getString(COLUMN_ADMINISTRATOREMAIL));
			s.setAdministratorPortrait(rs
					.getString(COLUMN_ADMINISTRATORPORTRAIT));
		} catch (Exception e) {

		}
		try {
			s.setCountry(rs.getString(COLUMN_COUNTRY));
			s.setProvince(rs.getString(COLUMN_PROVINCE));
			s.setCity(rs.getString(COLUMN_CITY));
			s.setSiteId(rs.getString(COLUMN_SITE_ID));

			s.setDetailAddress(rs.getString(COLUMN_DETAILADDRESS));
		} catch (Exception e) {

		}
		try {
			s.setIntroduction(rs.getString(COLUMN_INTRODUCTION));
			s.setCreateTime(rs.getDate(COLUMN_CREATETIME));
			s.setUpdateTime(rs.getDate(COLUMN_UPDATETIME));
		} catch (Exception e) {

		}
		try {
			s.setLatitude(rs.getDouble(COLUMN_LATITUDE));
			s.setLongitude(rs.getDouble(COLUMN_LONGITUDE));
		//	s.setPosition(new Position(rs.getDouble(COLUMN_LATITUDE),rs.getDouble(COLUMN_LONGITUDE)));
		} catch (Exception e) {

		}
		
		try {
			s.setStar(rs.getFloat(COLUMN_STAR));
			s.setVotes(rs.getInt(COLUMN_VOTES));
		} catch (Exception e) {

		}
		s.setVisitNum(rs.getInt(COLUMN_VISTNUM));
		try {
			s.setActivity(Activity.valueOf((rs.getString(COLUMN_ACTIVITY))));
		} catch (Exception e) {

		}

		return s;
	}
}
